<?php
class modalbum extends CI_Model{
	var $album = "gallery_album";

	function __construct()
	{
		parent::__construct();
	}
	
	function setData($id_album,$id_menu,$title_album,$tgl_album,$enable)
	{
		$this->id_album= $id_album;
		$this->id_menu= $id_menu;
		$this->title_album= $title_album;
		$this->tgl_album= $tgl_album;
		$this->enable= $enable;
	}
	
	function getList($page,$uri_segment){
		$sql="
			SELECT a.id_album,a.id_menu,title_album,tgl_album,enable,jml_picture,mn.nama_menu
			FROM gallery_album a
			left join setting_menu mn on a.id_menu = mn.id_menu
			left join (
			  select id_album,count(*) as jml_picture
			  from gallery_pictures
			  group by id_album
			) b on a.id_album = b.id_album
			limit ".$uri_segment.", ".$page."";
		$query=$this->db->query($sql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function create()
	{		
		$arrayData = array(
			'id_album'=>$this->id_album,
			'id_menu'=>$this->id_menu,
			'title_album'=>$this->title_album,
			'tgl_album'=>$this->tgl_album,
			'enable'=>$this->enable
		);
		return $this->db->insert($this->album, $arrayData);
	}
	
	function update($id_album)
	{
		$arrayData = array(
			'id_menu'=>$this->id_menu,
			'title_album'=>$this->title_album,
			'tgl_album'=>$this->tgl_album,
			'enable'=>$this->enable
		);
		$this->db->where('id_album', $id_album);
		return $this->db->update($this->album, $arrayData);
	}
        
	function remove($id_album)
	{
		$this->db->where('id_album', $id_album);
		return $this->db->delete($this->album);
	}	
	
	function detail($id_album)
	{
		$this->db->where('id_album', $id_album);
		$query = $this->db->get($this->album);	
		return $query->result_array();
	}
	
	function getListSearch($name){
		$sql="
			SELECT a.id_album,a.id_menu,title_album,tgl_album,enable,jml_picture,mn.nama_menu
			FROM gallery_album a
			left join setting_menu mn on a.id_menu = mn.id_menu
			left join (
			  select id_album,count(*) as jml_picture
			  from gallery_pictures
			  group by id_album
			) b on a.id_album = b.id_album
			where a.title_album like '%".$name."%'";
		$query=$this->db->query($sql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	//for web
	function getAlbumByMenu($id_menu)
	{
		$sql="
			SELECT a.id_album,a.id_menu,title_album,tgl_album,nama_picture,jml_picture
			FROM gallery_album a
			left join (
				select id_album,nama_picture,keterangan,count(*) as jml_picture
				from gallery_pictures
				group by id_album
			) b on a.id_album = b.id_album
			where a.id_menu = ".$id_menu." and a.enable = 1";
		$query=$this->db->query($sql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}
	}
}
?>